/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.ui.playlist.Playlist');

goog.require('kramerius.audio.ui.playlist.GlobalItemUpButton');
goog.require('kramerius.audio.ui.playlist.GlobalItemDownButton');
goog.require('kramerius.audio.ui.playlist.GlobalItemRemoveButton');
goog.require('kramerius.audio.ui.PlayerComponent');
goog.require('kramerius.audio.utils');

/** @const */ var PLAYLIST_CLASS = 'playlist';
/** @const */ var PLAYLIST_SHOWN = 'playlistShown';
/** @const */ var PLAYLIST_HIDDEN = 'playlistHidden';

/**
 * @constructor 
 * @param {Element} container Element containing this component.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 * The button controlls some aspects of player by calling controller methods.
 * Controller may also change state of the button.
 * @param {boolean} loggingEnabled whether logging should be enabled (for debugging)
 * 
 */
kramerius.audio.ui.playlist.Playlist = function(container, controller, loggingEnabled){
    kramerius.audio.ui.PlayerComponent.call(this, container, controller);
    this.loggingEnabled_ = loggingEnabled;
    this.headerEl_ = this.createHeaderElement_();
    this.listEl_ = this.createListElement_();
    this.createDom_(this.headerEl_, this.listEl_);
    this.items_ = [];
    this.globalItemUpButton_ = undefined;
    this.globalItemDownButton_ = undefined;
    //this.globalItemRemoveButton_ = undefined;
    this.createGlobalButtons_(this.headerEl_);
    this.selectedItem_ = undefined;
    this.activeItem_ = undefined;
    this.show();
    this.controller_.setPlaylist(this);
};
goog.inherits(kramerius.audio.ui.playlist.Playlist, kramerius.audio.ui.PlayerComponent);

/** @private */
kramerius.audio.ui.playlist.Playlist.prototype.createHeaderElement_ = function(){
    //    TODO: rename class
    var footer = goog.dom.createDom('div',{
        'class':'playlistHeader'    
    });
    return footer;
}

/** @private */
kramerius.audio.ui.playlist.Playlist.prototype.createListElement_ = function(){
    return goog.dom.createDom('div',{
        'class':'playlist-list'    
    });
}


/** @private */
kramerius.audio.ui.playlist.Playlist.prototype.createDom_ = function(headerEl, listEl){
    this.container_.appendChild(headerEl);
    this.container_.appendChild(listEl);
}

kramerius.audio.ui.playlist.Playlist.prototype.getSelectedItem = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        if (item.statusIsSelected()){
            return item;
        }
    }
    return undefined;
}

kramerius.audio.ui.playlist.Playlist.prototype.getActiveItem = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        if (item.statusIsActive()){
            return item;
        }
    }
    return undefined;
}


kramerius.audio.ui.playlist.Playlist.prototype.addItem = function(item){
    item.setPlaylist(this);
    this.items_[this.items_.length] = item; 
    this.listEl_.appendChild(item.getDom());
}


kramerius.audio.ui.playlist.Playlist.prototype.playItem = function(itemToPlay){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        item.setStatusNormal();
    }
    itemToPlay.setStatusActive();
    this.log_("playing " + itemToPlay.getTrack().getTitle());
    this.controller_.setTrack(itemToPlay.getTrack(), true);
    this.controller_.refreshPlayNextPlayPreviousButtons();
    this.refreshGlobaButtons();
    this.controller_.play();
}

kramerius.audio.ui.playlist.Playlist.prototype.selectItem = function(itemToSelect){
    this.log_("selected item " + itemToSelect.getTrack().getId());
    this.log_("playlist length: " + this.items_.length);
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //deselect previously selected item
        if (item.statusIsSelected())
            item.setStatusNormal();
    }
    
    //select only if not activated already
    if (itemToSelect.statusIsNormal()){
        this.log_("item status: normal");
        itemToSelect.setStatusSelected();
        this.refreshGlobaButtons();
    }    
}

kramerius.audio.ui.playlist.Playlist.prototype.switchItemWithPrevious = function(item){
    for(i=0;i<this.items_.length;i++){
        var itemIterated = this.items_[i];
        if (itemIterated.getId() == item.getId()){
            var tmp = this.items_[i-1];
            this.items_[i-1] = item;
            this.items_[i] = tmp;
        }
    }
    this.refreshGlobaButtons();
    this.controller_.refreshPlayNextPlayPreviousButtons();
}

kramerius.audio.ui.playlist.Playlist.prototype.switchItemWithNext = function(item){
    for(i=0;i<this.items_.length;i++){
        var itemIterated = this.items_[i];
        if (itemIterated.getId() == item.getId()){
            var tmp = this.items_[i+1];
            this.items_[i+1] = item;
            this.items_[i] = tmp;
            //pozor na tohle. 
            //Bez breaku se vymeni a if znovu uspeje
            //seznam items tak poroste do nekonecna
            break;
        }
    }
    this.refreshGlobaButtons();
    this.controller_.refreshPlayNextPlayPreviousButtons();
}

kramerius.audio.ui.playlist.Playlist.prototype.removeItem = function(item){
    for(i=0;i<this.items_.length;i++){
        var itemIterated = this.items_[i];
        if (itemIterated.getId() == item.getId()){
            //console.log("items before removal of " + i + ": ");
            //console.log(this.items_);
            kramerius.audio.utils.removeArray(this.items_,i);
            //console.log("items after removal of " + i + ": ");
            //console.log(this.items_);
            if (item.statusIsActive()){
                this.controller_.clear();
            }
            break;
        }
    }
    this.refreshGlobaButtons();   
}


kramerius.audio.ui.playlist.Playlist.prototype.refreshGlobaButtons = function(){
    var selectedItem = this.getSelectedItem();
    if (selectedItem == null){
        selectedItem = this.getActiveItem();
    }
    
    if (selectedItem == undefined){
        //deactivate all global buttons
        this.deactivateItemUpButton();
        this.deactivateItemDownButton();
    //this.deactivateItemRemoveButton();
    }else{
        var selectedItemIndex = undefined;
        for(i=0;i<this.items_.length;i++){
            var item = this.items_[i];
            if (item.getId() == selectedItem.getId()){
                this.log_("refreshGlobalButtons: selected item index: " + i);
                selectedItemIndex = i;
            }
        }
        //activate/deactivate global item up button
        if (selectedItemIndex != 0){
            this.activateItemUpButton();
        }else{
            this.deactivateItemUpButton();
        }
        //activate/deactivate global item down button
        if (selectedItemIndex != this.items_.length -1){
            this.activateItemDownButton();
        }else{
            this.deactivateItemDownButton();
        }
    //activate global item remove button
    //this.activateItemRemoveButton();
    }
}


kramerius.audio.ui.playlist.Playlist.prototype.getNextItemToPlay = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //  console.log(i);
        if (item.statusIsActive() && (i+1)!=this.items_.length){
            return this.items_[i+1];
        }
    }
    return null;
}

kramerius.audio.ui.playlist.Playlist.prototype.getPreviousItemToPlay = function(){
    for(i=0;i<this.items_.length;i++){
        var item = this.items_[i];
        //  console.log(i);
        if (item.statusIsActive() && i!=0){
            return this.items_[i-1];
        }
    }
    return null;
}


kramerius.audio.ui.playlist.Playlist.prototype.createGlobalButtons_ = function(buttonsContainer){
    //UP
    var btnUpContainer = goog.dom.createDom('span');
    buttonsContainer.appendChild(btnUpContainer);
    this.globalItemUpButton_ = new kramerius.audio.ui.playlist.GlobalItemUpButton(btnUpContainer, this, this.loggingEnabled_);
    //DOWN
    var btnDownContainer = goog.dom.createDom('span');
    buttonsContainer.appendChild(btnDownContainer);
    this.globalItemDownButton_ = new kramerius.audio.ui.playlist.GlobalItemDownButton(btnDownContainer, this, this.loggingEnabled_);
//REMOVE
//    var btnRemoveContainer = goog.dom.createDom('span');
//    buttonsContainer.appendChild(btnRemoveContainer);
//    this.globalItemRemoveButton_ = new kramerius.audio.ui.playlist.GlobalItemRemoveButton(btnRemoveContainer, this);
}

kramerius.audio.ui.playlist.Playlist.prototype.activateItemUpButton = function(){
    //this.log_("activating global item up button");
    this.globalItemUpButton_.setStateEnabled();
}

kramerius.audio.ui.playlist.Playlist.prototype.deactivateItemUpButton = function(){
    //this.log_("deactivating global item up button");
    this.globalItemUpButton_.setStateDisabled();
}

kramerius.audio.ui.playlist.Playlist.prototype.activateItemDownButton = function(){
    //this.log_("activating global item down button");
    this.globalItemDownButton_.setStateEnabled();
}

kramerius.audio.ui.playlist.Playlist.prototype.deactivateItemDownButton = function(){
    //this.log_("deactivating global item down button");
    this.globalItemDownButton_.setStateDisabled();
}

//kramerius.audio.ui.playlist.Playlist.prototype.activateItemRemoveButton = function(){
//    this.log_("activating global item remove button");
//    this.globalItemRemoveButton_.enable();
//}
//
//kramerius.audio.ui.playlist.Playlist.prototype.deactivateItemRemoveButton = function(){
//    this.log_("deactivating global item remove button");
//    this.globalItemRemoveButton_.disable();
//}

kramerius.audio.ui.playlist.Playlist.prototype.show = function(){
    this.setContainerClass_(PLAYLIST_SHOWN);
}

kramerius.audio.ui.playlist.Playlist.prototype.hide = function(){
    this.setContainerClass_(PLAYLIST_HIDDEN);
}

/** @protected */
kramerius.audio.ui.playlist.Playlist.prototype.setContainerClass_ = function(playlistClass){
    this.container_.setAttribute("class", PLAYLIST_CLASS + ' ' + playlistClass);
    this.containerClass_ = playlistClass;
};


/** @private */
kramerius.audio.ui.playlist.Playlist.prototype.log_ = function(text){
    if (this.loggingEnabled_){
        console.log("Playlist: " + text);
    }    
}












